import { _decorator, Component, instantiate, Node, Prefab, ScrollView, Size, UITransform, Vec3 } from 'cc';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { RankItem } from './RankItem';
import { v3 } from 'cc';
import { WECHAT } from 'cc/env';
import WXRankList from '../../../framework/core/base/WXRankList';
import { GameDataCenter } from '../../../framework/core/base/GameDataCenter';
import { MonoUIPanel } from '../../../common/src/mono/MonoUIPanel';
const { ccclass, property } = _decorator;

@ccclass('RankPanel')
export class RankPanel extends MonoUIPanel {
    @property({ type: ScrollView, displayName: "总榜未选中" })
    scrollView: ScrollView;
    
    @property({ type: Node, displayName: "好友排行" })
    friendRank: Node;

    @property ( {type: Prefab, displayName: "列表的预制体"} )
    randItem: Prefab

    private skins = [
        {
            "rank": 1,
            "name": "慕无"
        },
        {
            "rank": 2,
            "name": "幻翼飘雪"
        },
        {
            "rank": 3,
            "name": "房东的猫猫"
        },
        {
            "rank": 4,
            "name": "屿下"
        },
        {
            "rank": 5,
            "name": "情浅缘深"
        },
        {
            "rank": 6,
            "name": "伤停补时"
        },
        {
            "rank": 7,
            "name": "荏苒"
        },
        {
            "rank": 8,
            "name": "360"
        },
        {
            "rank": 9,
            "name": "江畔柳前堤"
        },
        {
            "rank": 10,
            "name": "话别已深秋"
        },
    ]
    
    private isInit = false

    onLoad(): void {
        var offY = 0
        for (var i = 0; i < this.skins.length; i++) {           
            var rankItem = instantiate( this.randItem )
            rankItem.position = new Vec3(0, -85 * i - 45, 0);
            rankItem.getComponent(RankItem).UpdateInfo( this.skins[i] )
            this.scrollView.content.addChild ( rankItem )

            offY = rankItem.position.y - 50
        }

        this.scrollView.content.getComponent(UITransform).contentSize = new Size(575, offY * -1)
    }
    

    // ---------------------- 初始化 ---------------------- //
    private initFriendRank() {
        if (WECHAT) {
            
            let callback = () => {
                WXRankList.instance.loadScoreRank();
            }

            GameDataCenter.setUserCloudStorage(callback);
            this.isInit = true
        }

    }

    // --------------------- 按钮事件 --------------------- //
    private onFriendRankClicked() {
        this.friendRank.active = true
        this.scrollView.node.active = false
        
        if (this.isInit == false) {
            this.initFriendRank()
        }
    }

    private onAllRankClicked() {
        this.friendRank.active = false
        this.scrollView.node.active = true
    }

    /** 关闭面板. */
    private async onClosePanel() {
        AudioManager.playButton();
        await this.onExitAnimation();
        UIManager.close("rank:res/prefab/RankDialog");

        //gg.systemEvent.emit(EVENT_NAMES.SHOW_START_BUTTONS, true)
    }
}


